/************************************************************

This object wille create a remote access to a server using XMLHTTPRequest

the init parameters are:

- url					: indicate the relative or absolute uri to call
- withWaitWindow			: if set to true will display a wait popup during the load
- callBackObjectOnSuccess: object who have the onsucces callback method 
- callBackObjectOnFailure: object who have the onfailure callback method 
- canBeCancel			: if set to true, the request can be abort
- method					: call method (get or post)
- onSuccess				: onsucces callback method  
- OnFailure				: onfailure callback method  
*************************************************************/
rialto.io={};

rialto.lang.link = function(obj, method){
	var object=obj;
	var fcn = method;
	return function() {
		return fcn.apply(object, arguments);
	}
}

rialto.io.AjaxRequest=function(objPar){
	
	
	//Default values
	this.url					= objPar.url;
	this.withWaitWindow			= true;
	this.callBackObjectOnSuccess= null;
	this.callBackObjectOnFailure= null;
	this.canBeCancel			= true;
	
	this.config					= {
			 asynchronous	: true
			,method			: 'get'
	
			,onSuccess		: this.reportSuccess
			,onFailure		: this.reportError
	};
	
	if (objPar != null){               
        if (objPar.url){
            this.url = objPar.url;        
        }
        if (objPar.callBackObjectOnSuccess){
            this.callBackObjectOnSuccess = objPar.callBackObjectOnSuccess;        
        }
        if (objPar.callBackObjectOnFailure){
            this.callBackObjectOnFailure = objPar.callBackObjectOnFailure;        
        }
        if (objPar.canBeCancel!=null && (objPar.canBeCancel==true || objPar.canBeCancel==false)){
            this.canBeCancel = objPar.canBeCancel;        
        } 
        if (objPar.withWaitWindow!=null && (objPar.withWaitWindow==true || objPar.withWaitWindow==false)){
            this.withWaitWindow = objPar.withWaitWindow;        
        }        
        if (objPar.method!='' && objPar.method!=null && (objPar.method=="post" || objPar.method=="get")){
            this.config.method = objPar.method;        
        }
        if (objPar.onSuccess){
            if(this.callBackObjectOnSuccess){
				this.config.onSuccess =rialto.lang.link(this.callBackObjectOnSuccess,objPar.onSuccess);		
			}
			else{
				this.config.onSuccess =objPar.onSuccess;
			}        
        }
        if (objPar.onFailure){
            if(this.callBackObjectOnFailure){
				this.config.onFailure =rialto.lang.link(this.callBackObjectOnFailure,objPar.onFailure);		
			}
			else{
				this.config.onFailure =objPar.onFailure;
			}        
        }
        
         
    }   
	
	
	if(this.withWaitWindow){		
		this.config.onLoading= rialto.lang.link(this,this.displayWaitWindow);		
	 }
	 
	 	
	//get XMLHttpRequest object
	this.xhttpr = null; 	 
	
	
	if(window.XMLHttpRequest) { // Firefox & other
		/*try {
		    netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
		}
		catch(e) {alert(e);};*/
		
 	    this.xhttpr = new XMLHttpRequest(); 
	}
	
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
            this.xhttpr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            this.xhttpr = new ActiveXObject("Microsoft.XMLHTTP");
        }
	}
	else { // XMLHttpRequest non support? par le navigateur 
	   this.xhttpr = false; 
	}
	this.status = ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
}


rialto.io.AjaxRequest.prototype={

		load : function(parameters){

				var url = this.url;
						
				this.config.parameters = parameters;					

				try {			
      				if (	  this.config.method == 'get' 
      					  &&  this.config.parameters.length > 0)
    					url += (url.match(/\?/) ? '&' : '?') + parameters;
						
				    this.xhttpr.open(this.config.method, url,this.config.asynchronous);
					//IE BUGG WITH THE CACHE
					this.xhttpr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
				    if (this.config.method == 'post') {
				    	this.xhttpr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			    	}
			    	
			      	if (this.config.asynchronous) {			        
						this.xhttpr.onreadystatechange = rialto.lang.link(this,this.respondToReadyState);
			      	}

					this.xhttpr.send(this.config.method == 'post' ? this.config.parameters : null);
			
			    } catch (e) {alert(e+'\n (url :'+url+')');}				
		}
		,
			
		responseIsSuccess : function() {	    		    	
	    	return (this.xhttpr.status == undefined  ||  
	    			this.xhttpr.status == 0    ||  
	    			(this.xhttpr.status >= 200 && this.xhttpr.status < 300));
		},

		responseIsFailure : function() {
	    	return !this.responseIsSuccess();
		},
		
		respondToReadyState : function() {
 		    
 		    var readyState 	= this.xhttpr.readyState;
		    var status 		= this.status[readyState] ;
		   
 		    switch (status) {
				case 'Complete': 
				    if(this.withWaitWindow){this.fen.masqueFen();};
					try {					
					     this.config['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')](this.xhttpr);		    
				    } catch (e) {
				    }
		    		break;
		    		
/*		    	case 'Loading':
    				try {
				      this.config['onLoading'](this.xhttpr)
				    } catch (e) {				    	
				    }
   					break;	*/
   					
   				default:
	   				try {
				      this.config['on'+status](this.xhttpr)
				    } catch (e) {				    	
				    }
   					break;	
		    }
	  	}
	  	,
				
		abort : function(){						
			if ( this.canBeCancel ){ this.xhttpr.abort(); }
  		}
  		,
  		displayWaitWindow : function(){  		  					   
    		if(this.fen){
	    		this.fen.display();
    		}
    		else{    		    		
			    var oThis=this;
			    //taille disponible
			    widthGlobal=document.body.clientWidth;
			    heightGlobal=document.body.clientHeight;       
			    var top=(heightGlobal/2)-60;
			    var left=(widthGlobal/2)-100;
			
			    this.fen = new rialto.widget.Dialog("fen",top,left,200,90,""," ","transparent");
			    var divLib=document.createElement("DIV");    
			    divLib.className="libelle2";
			    divLib.style.position="absolute";
			    divLib.style.top="5px";        
			    divLib.style.left="65px";        
			    divLib.style.width="100%";                
			    divLib.innerHTML="LOADING";
			    this.fen.add(divLib);   
			    //image
			    var img=document.createElement("IMG");        
			    img.src=rialtoConfig.pathRialtoE + "images/sablier.gif";
			    img.style.position="absolute";
			    img.style.height='35px';
			    img.style.width='27px';
			    img.style.left='75px';
			    img.style.top='22px';
			    this.fen.add(img);
			
				if(this.canBeCancel){
				    //Bouton annuler
				    var BANN = new rialto.widget.Button(60,50,"CANCEL");
				
				    BANN.onclick=function () {
				        oThis.abort();
				        oThis.fen.masqueFen();
				    }
				    this.fen.add(BANN);
				}	     	    
			}
		}
	,	
	//Default function
	reportSuccess : function (request){
		var div = document.createElement("DIV");
		div.innerHTML = request.responseText;
		var fen = new rialto.widget.objFenData(600,400,div,'Response')
	}	
	,
	reportError : function (request){		
		var div=document.createElement("DIV");
		div.innerHTML=request.responseText;
		var fen=new rialto.widget.objFenData(600,400,div,'Sorry. There was an error.')
	}						
}

	


